package fei.zhou.springboot4.demo3;

import java.util.concurrent.Executor;

import fei.zhou.springboot4.demo2.DemoService;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: TaskConfig
 * @date 2021/1/20 11:38
 * @Verson 1.0 -2021/1/20 11:38
 * @see
 */
@Configuration
@ComponentScan("fei.zhou.springboot4.demo3")
@EnableAsync // 1 开启异步
public class TaskTest implements AsyncConfigurer {
    
    @Override
    public Executor getAsyncExecutor() {
        // 设置线程池
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        
        // 核心线程数
        taskExecutor.setCorePoolSize(25);
        // 最大线程数
        taskExecutor.setMaxPoolSize(50);
        // 阻塞队列
        taskExecutor.setQueueCapacity(100);
        taskExecutor.initialize();
        return taskExecutor;
    }
    
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskTest.class);
        DemoService2 demoService = context.getBean(DemoService2.class);
        for (int i = 0; i < 10; i++) {
            demoService.asyncTask1(i);
            
        }
        context.close();
    }
}